/*var authApp=angular.module('authapp',['ngFileUpload',"ngCookies"]);
authApp.directive('authheader',function($compile,$sce){
	return {
        restrict: 'A',
        template : ('<header><div class="head-part"><p>江苏省师范类专业认证系统</p></div></header>'),
        replace: true,
        transclude: true,
    };
})
authApp.directive('authfooter',function($compile,$sce){
	return {
        restrict: 'A',
        template : ('<footer><p>Copyright @ 2016版权所有：江苏省教育评估院    联系地址：南京市北京西路15号  教育大厦26楼</p><p>技术支持：江苏省经贸职业技术学院</p></footer>'),
        replace: true,
        transclude: true,
    };
})

authApp.controller("headerControl",function($scope,$sce){
	$scope.changePsd=function(){
		alert("aaaaaa");
	}
	
})
authApp.factory('$splice',function(){
	  return function(str,length,isHasDots){
	    var str1="";
	      if(str.length >= length) {
	        if(isHasDots){
	          str1=str.substring(0,length)+"...";
	        }else{
	          str1=str.substring(0,length);
	        }
	      }else {
	        str1=str;
	      }
	      return str1;
	  };
	})
authApp.factory('$getQueryString',function($location){
	return function(name){
		var array=$location.absUrl().split("?")[1].split("&");
		var json="";
		for(var i=0;i<array.length;i++){
			if(i!=(array.length-1)&&array.length!=1)
				json+="{"+array[i].split("=")[0]+":"+array[i].split("=")[1];
			else if(i==(array.length-1)&&array.length!=1)
				json+=array[i].split("=")[0]+":"+array[i].split("=")[1]+"}";
			else if(array.length==1)
				json+="{"+array[i].split("=")[0]+":"+array[i].split("=")[1]+"}";
		}
		eval("var jsonr="+json);
		var nval="jsonr."+name;
		eval("xval="+nval);
		return xval
	}
})
*/
authApp.value('page','declaration'); 
authApp.controller("fillApplyController",function($scope,$http,$location,$getQueryString,Upload, $timeout,$cookieStore,$cookies){
	$scope.isUpload=true;
	$scope.hideUpload=false;
	$scope.zzmlList=[{'xh':"",'ml':"",'time':"",'isOpen':"",'file':""}];
	$scope.schoolId=$getQueryString("schoolId");
	$scope.state=$getQueryString("state");	
	if($scope.state=="1"){
		$scope.isUpload=false;
	}	
	$scope.turnTo=function(){
		location.href="material_apply_list.html?pid="+$scope.schoolId;
	}
	$scope.getTwiceById=function(){
		var jsonData={"pid":$getQueryString("pid")};
		$http({
			method:"post",
			url:apis.queryTwiceById,
			data:jsonData,
		}).success(function(response){
			$scope.TData=response.data[0];
		/*	angular.forEach($scope.TData.listContent,function(data,index,array){
				console.log(data);
			});*/
		})
	}
	$scope.getTwiceById();
	
	$scope.uploadFiles = function(file, errFiles) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];
        if (file) {
            file.upload = Upload.upload({
                url: apis.uploadTask,
                data: {file: file}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                	if(response.data.code=="000000")
                		file.result = response.data;
                	else
                		alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * 
                                         evt.loaded / evt.total));
            });
        }   
    }
	
	$scope.uploadAll=function(){
		if(window.confirm("是否确认保存数据？")){
			var dataArr="{";
			var isAllow=true;
			var detailLevelId="";
			var detailLevel="";
			
			angular.forEach($scope.TData.listContent,function(data,index,array){
				if(array.length==(index+1)){
					if(!data.value){
						isAllow=false;
						return
					}
					detailLevelId+=data.pid;
					detailLevel+=data.value;
					dataArr+=data.pid+":"+data.value+"}";
				}
				else{
					if(!data.value){
						isAllow=false;
						return
					}
					detailLevelId+=data.pid+",";
					detailLevel+=data.value+",";
				}
			})
			var filename="";
			var dealTime="";
			var open="";
			var filepath="";
			
			angular.forEach($scope.zzmlList,function(data,index,array){
				console.log(data.file);
				if(array.length==(index+1)){
					if(!data.time||!data.isOpen||!data.file){
						console.log(data.file);
						isAllow=false;
						return
					}
					dealTime+=data.time;
					open+=data.isOpen;
					filename+=data.file.name;
					filepath+=data.file.result.data;
				}else{
					if(!data.time||!data.isOpen||!data.file){
						isAllow=false;
						return
					}
					dataArr+=data.pid+":"+data.value+",";
					dealTime+=data.time+",";
					open+=data.isOpen+",";
					filename+=data.file.name+",";					
					filepath+=data.file.result.data+",";
				}
			})
			if(isAllow){
				var insertitemLevelData={'itemId':$getQueryString("pid"),'detailLevelId':detailLevelId,'detailLevel':detailLevel};
				$http({
					url:apis.insertitemLevel,
					method:"post",
					data:insertitemLevelData,
				}).success(function(){
					var insertSchoolItemData={'itemId':$getQueryString("pid"),'reason':$scope.mainReason,'selfinstr':$scope.selfDescript,'singlelevel':$scope.singleLevel};
					$http({
						url:apis.insertSchoolItem,
						method:"post",
						data:insertSchoolItemData,
					}).success(function(){
						var insertProofData={'itemId':$getQueryString("pid"),'filename':filename,'dealTime':dealTime,'open':open,'filepath':filepath};
						$http({
							url:apis.insertProof,
							method:"post",
							data:insertProofData,
						}).success(function(){
							alert("保存数据成功！");
							location.href="material_apply_auth.html?pid="+$scope.Fid+"&state="+$scope.state;
						})
					})
				})
			}else{
				alert("信息未填充完整");
			}
		}
	}
	
	$scope.addOneC=function(){
		if($scope.zzmlList.length<4){
			var zzymSingleList={'xh':"",'ml':"",'time':"",'isOpen':"",'file':""};
			$scope.zzmlList.push(zzymSingleList);
		}else{
			alert("最多四条");
		}
	}
	
	/*if($getQueryString("action")=="read")*/
	$scope.getDataTab = function(){
		/*$scope.hideUpload=true;*/
		$http({
			url:apis.queryAllTwiceInfoById1,
			method:"post",
			data:{'schoolId':$getQueryString("schoolId"),'itemId':$getQueryString("pid")},
		}).success(function(response){
			$scope.ddData=response.data;
			$scope.selfDescript=response.data.selfdesc;
			$scope.mainReason=response.data.mainReason;
			$scope.singleLevel=response.data.singleLevel;
			angular.forEach(response.data.detailItem,function(data,index,array){
				$scope.TData.listContent[index].value=data.level;
			})
			angular.forEach(response.data.fileItem,function(data,index,array){
				if(index>0){
					var jsons={'xh':"",'ml':"",'time':"",'isOpen':"",'file':""};
					$scope.zzmlList.push(jsons);
					$scope.zzmlList[index].time=data.dealtime;
					$scope.zzmlList[index].isOpen=data.open;
					$scope.zzmlList[index].file={};
					$scope.zzmlList[index].file.name=data.filename;
					$scope.zzmlList[index].file.result={};
					$scope.zzmlList[index].file.result.data=data.filepath;
					console.log($scope.zzmlList[index]);
				}else{
					$scope.zzmlList[index].time=data.dealtime;
					$scope.zzmlList[index].isOpen=data.open;
					$scope.zzmlList[index].file={};
					$scope.zzmlList[index].file.name=data.filename;
					$scope.zzmlList[index].file.result={};
					$scope.zzmlList[index].file.result.data=data.filepath;
					console.log($scope.zzmlList[index]);
				}
				
				//$scope.zzmlList=[{'xh':"",'ml':"",'time':"",'isOpen':"",'file':""}];
				//$scope.TData.listContent[index].value=data.level;
			})
			//$scope.TData=response.data.detailItem;
		})		
	}
	
	$scope.getDataTab();
	
	$scope.minusOneColl=function(index){
		if($scope.zzmlList.length<2){
			alert("不可全部删除");
		}else{
			$scope.zzmlList.splice(index,1);
		}
	}
})